<?php
    class ApiCenter_ProductApi_IndexController extends Mage_Core_Controller_Front_Action{
		
		
        private static $_allData = array();
		
        
        public function getCategoryTreeAction(){
			
		$storeid = 0;	
        $parent_id = 2;
        $collection = Mage::getModel('catalog/category')->getCollection();
        
        $collection->addFieldToFilter('parent_id',array('gteq' => array($parent_id)));
        $collection->getSelect()
        ->joinLeft(array('f' => 'catalog_category_entity_varchar'),
            'e.entity_id = f.entity_id',
            array(
                "text"=>"f.value",
            ))
		 ->joinLeft(array('g' => 'catalog_category_entity_url_key'),
            'e.entity_id = g.entity_id',
            array(
                "urlkey"=>"g.value",
            ))	
            ->where("f.attribute_ID = 41 AND f.store_id = ".$storeid." AND g.store_id = ".$storeid);
        $nodes = $collection->getData();
		$baseurl =  Mage::getBaseUrl();
		self::$_allData = $nodes;
        $topNodes = self::getTopNodes($nodes,$baseurl);
        foreach ($topNodes as $node) {
            $tree[] = self::getSubNodes($node,$baseurl,$node['urlkey']."/");
        }
        echo '<hr/>', microtime(true), '<hr/>';
		echo "<pre/>";
		print_r($tree);
		$html.="<ul>";
		$html.=self::displayTree($tree);
        $html.="</ul>";
		echo $html;
    }

     public static function displayTree($tree,$leven =0)
     {
      
        foreach ($tree as $node) {
             $str .= '<li><a href = "'.$node['url'].'" target="_blank">';
             $str.= $node['name'];
             $str.="</a>";
             
				if(!$node['is_leaf']){
				$str .= '<ul class="level">';
                $str .=  self::displayTree($node['children'], $leven+1);
                $str .= '</ul>';
				}
			$str .='</li>';
       }
	   
	   return $str;
    }
    private static function getTopNodes($nodes,$baseurl)
    {
        $data = array();

        foreach ($nodes as $row)
        {
            if ($row['parent_id'] == '2')
            {
                $node = array();
                $node['id'] = $row['entity_id'];
                $node['name'] = $row['text'];
				$node['url'] = $baseurl.$row['urlkey'];
				$node['urlkey'] = $row['urlkey'];
                $node['is_leaf'] = true;
                $node['children'] = array();
                $data[] = $node;
            }
        }
        return $data;
    }
    private static function getSubNodes(array $node,$baseurl="",$urlkey="")
    {
        foreach (self::$_allData as $row)
        {
            if ($row['parent_id'] == $node['id'])
            {
                $matchurl = $urlkey.$row['urlkey']."/";
                $subNode = array();
                $subNode['id'] = $row['entity_id'];
                $subNode['name'] = $row['text'];
				$subNode['url'] = $baseurl.$matchurl;
                $subNode['is_leaf'] = true;
                $subNode['children'] = array();
                $node['children'][] = self::getSubNodes($subNode,$baseurl,$matchurl);
            }
        }
        if (count($node['children']) > 0) {
            $node['is_leaf'] = false;
        }
        return $node;
    }
    }